package com.hotau.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

public class CalendarUtil {

	public static final String dfMM_dd_EEE_Str = "MM.dd EEE";
	public static final String dfyyyyMMdd_Str = "yyyyMMdd";
	public static final String dfyyyy_MM_dd_Str = "yyyy-MM-dd";
	public static final String dfyyyy_MM_dd_HH_mm_Str = "yyyy-MM-dd HH:mm";
	public static final String dfyyyyMMddHHmmss_Str = "yyyyMMddHHmmss";
	public static SimpleDateFormat dfyyyyMMdd = new SimpleDateFormat(
			dfyyyyMMdd_Str);
	public static SimpleDateFormat dfyyyy_MM_dd = new SimpleDateFormat(
			dfyyyy_MM_dd_Str);
	public static SimpleDateFormat dfyyyy_MM_dd_HH_mm = new SimpleDateFormat(
			dfyyyy_MM_dd_HH_mm_Str);
	public static SimpleDateFormat dfyyyyMMddHHmmss = new SimpleDateFormat(
			dfyyyyMMddHHmmss_Str);

	public static Date addDay(Date priDate, int dateOffset) {
		Calendar day = Calendar.getInstance();
		day.setTime(priDate);
		day.add(Calendar.DATE, dateOffset);
		return day.getTime();
	}

	public static Date addMin(Date priDate, int minOffset) {
		Calendar day = Calendar.getInstance();
		day.setTime(priDate);
		day.add(Calendar.MINUTE, minOffset);
		return day.getTime();
	}

	public static Calendar getCalendar4yyyy_MM_dd(String dateStr) {
		// Log.d("mytag", "CalendarUtil getCalendar4yyyy_MM_dd=" + dateStr);
		Calendar day = Calendar.getInstance();
		try {
			day.setTime(dfyyyy_MM_dd.parse(dateStr));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * 
	 * dayBeTest 是否在 day 至少前一天
	 * 
	 */
	public static boolean isDayBefore(Date dayBeTest, Date day) {

		Calendar dayCalendar = Calendar.getInstance();
		try {
			dayCalendar.setTime(dfyyyy_MM_dd.parse(dfyyyy_MM_dd.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		dayCalendar.add(Calendar.DATE, -1);
		dayCalendar.add(Calendar.HOUR, 23);
		dayCalendar.add(Calendar.MINUTE, 59);
		dayCalendar.add(Calendar.SECOND, 55);

		Log.d("mytag", "isDayBefore dayBeTest=" + dayBeTest + " day=" + day
				+ " dayCalendar=" + dayCalendar);

		return dayBeTest.before(dayCalendar.getTime());

	}

	/**
	 * dayBeTest 是否在 day 至少后一天
	 */
	public static boolean isDayAfter(Date dayBeTest, Date day) {

		Calendar dayCalendar = Calendar.getInstance();
		try {
			dayCalendar.setTime(dfyyyy_MM_dd.parse(dfyyyy_MM_dd.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		dayCalendar.add(Calendar.DATE, 1);
		dayCalendar.add(Calendar.SECOND, -5);

		Log.d("mytag", "isDayAfter dayBeTest=" + dayBeTest + " day=" + day
				+ " dayCalendar=" + dayCalendar);

		return dayBeTest.after(dayCalendar.getTime());

	}

	public static String correctyyyy_MM_ddDate(String str) {
		try {
			Date date = dfyyyyMMdd.parse(str);
			Calendar day = Calendar.getInstance();
			day.setTime(date);
			return dfyyyy_MM_dd.format(day.getTime());
		} catch (Exception ex) {
			return str;
		}
	}
	
	public static String getTodayStr()
	{
	    return dfyyyyMMdd.format(new Date());
	}

	public static String getTodayyyyyMMddHHmmssStr()
	{
	    return dfyyyyMMddHHmmss.format(new Date());
	}
}
